package name.elftesting.vncelf;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import name.elftesting.rfbplayer.RfbPlayer;
import name.elftesting.vnctest.VncElf;

/**
 * 
 * @author Xiaoping Wu
 * 
 */
public class VncPlayerFrame extends JFrame {
	private static final long serialVersionUID = -3374450823823567437L;

	private static final JLabel usernameLabel = new JLabel("Record Files:");
	private static final JTextField usernameInput = new JTextField();
	 private static String fileChoosePath="";
	
	JButton loginButton = new JButton("Play");
	JButton exitButton = new JButton("Cancel");
	JButton fileChoose = new JButton("Browser");
	VncViewer viewer=new VncViewer();
	public VncPlayerFrame(final VncViewer viewer) {
		this.viewer=viewer;
	
		setSize(new Dimension(420, 180));
		setTitle("Rfb file player");
		setLayout(null);
	
		initUI();
		ImageIcon image = new ImageIcon(this.getClass().getResource(
				"/images/vnc.png"));

		this.setIconImage(image.getImage());
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		Dimension screen = getToolkit().getScreenSize();
		// pack();
		//setLocation((screen.width - getSize().width) / 2,
		//		(screen.height - getSize().height) / 2);

		
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName = usernameInput.getText().trim();
				
				if (userName.equals("")) {
					JOptionPane.showMessageDialog(VncPlayerFrame.this,
							"Please choose rfb file address");
					return;
				}
				
				dispose();
				fileChoosePath=userName;
				
				String[] argv = new String[] { "URL","file:///"+ userName };
				//fileChoosePath
				RfbPlayer p = new RfbPlayer();
				p.mainArgs = argv;
				p.inAnApplet = false;
				p.inSeparateFrame = true;

				p.init();
				p.start();
			}
		});

		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		fileChoose.addActionListener(new ActionListener(){
			public void doOpen() {

				try {
					JFileChooser jfc = new JFileChooser("");
					FileFilter ffc = new FileNameExtensionFilter("fbs", "FBS",
							"fbs file");
					jfc.addChoosableFileFilter(ffc);

					int i = jfc.showOpenDialog(viewer);
					File file = null;
					if (i == JFileChooser.APPROVE_OPTION) {
						file = jfc.getSelectedFile();
					}

				
					try {

						usernameInput.setText(file.getAbsolutePath());
					

					} catch (Exception e) {
						JOptionPane.showMessageDialog(viewer, "Open file failed!");
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} catch (Exception exe) {
					exe.printStackTrace();
				}
			}
			public void actionPerformed(ActionEvent e){
				this.doOpen();
			}
		});
	}
	
	

	

	private void initUI() {
		
		usernameLabel.setBounds(10, 10, 90, 21);
		usernameInput.setBounds(100, 10, 200, 21);
		fileChoose.setBounds(310,10,90,21);
		loginButton.setBounds(90, 100, 100, 21);
		exitButton.setBounds(200, 100, 100, 21);
		usernameInput.setEditable(true);
	
		add(usernameLabel);
		add(usernameInput);
		add(fileChoose);
		add(loginButton);
		add(exitButton);
	}

	public static void main(String[] args) {
		new VncPlayerFrame(null);
	}
}
